/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.samples.explorer.client;

import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.widget.Button;
import net.mygwt.ui.client.widget.ButtonBar;
import net.mygwt.ui.client.widget.IconButton;
import net.mygwt.ui.client.widget.Info;
import net.mygwt.ui.client.widget.Item;
import net.mygwt.ui.client.widget.ToolBar;
import net.mygwt.ui.client.widget.ToolItem;
import net.mygwt.ui.client.widget.WidgetContainer;
import net.mygwt.ui.client.widget.layout.FillLayout;
import net.mygwt.ui.client.widget.layout.RowData;
import net.mygwt.ui.client.widget.layout.RowLayout;
import net.mygwt.ui.client.widget.menu.Menu;
import net.mygwt.ui.client.widget.menu.MenuItem;
import net.mygwt.ux.client.action.Action;
import net.mygwt.ux.client.action.ActionRegistry;
import net.mygwt.ux.client.action.IActionListener;

import com.google.gwt.user.client.ui.HorizontalPanel;

/**
 * 
 * @author ggeorg
 * 
 */
public class ActionsPage extends Page implements IActionListener {

  public ActionsPage() {
    super();

    ActionRegistry.register(new Action("action:email", "Email", "Email tool tip",
        "icon-email-add"));
    ActionRegistry.register(new Action("action:appointment", "Appointment",
        "Appointment tool tip", "icon-date-add"));
    ActionRegistry.register(new Action("action:contact", "Contact", "Contact tool tip",
        "icon-contact-add"));
    ActionRegistry.register(new Action("action:new", "New", "New tool tip",
        "icon-email-add"));
    ActionRegistry.register(new Action("action:disabled", "Disabled", "Disabled tool tip"));
  }

  public void initActions() {
    Action action = ActionRegistry.get("action:email");
    action.addActionListener(this);
    action.setEnabled(true);

    action = ActionRegistry.get("action:appointment");
    action.addActionListener(this);
    action.setEnabled(true);

    action = ActionRegistry.get("action:new");
    action.setEnabled(true);
  }

  protected void createWidget(WidgetContainer container) {
    initActions();

    // TODO
    // Listener checkListener = new Listener() {
    // public void handleEvent(BaseEvent be) {
    // MenuItem item = (MenuItem) be.widget;
    // String s = item.isSelected() ? "checked" : "unchecked";
    // String[] values = new String[] {s, item.getText()};
    // Info.show("Item Check", "You {0} the '{1}' menu item.", values);
    // }
    // };

    ToolBar toolbar = new ToolBar();

    Menu newMenu = new Menu();

    MenuItem menuItem = (MenuItem) ActionRegistry.get("action:email").createItem(
        Action.MENUITEM, Style.PUSH);
    newMenu.add(menuItem);

    menuItem = (MenuItem) ActionRegistry.get("action:appointment").createItem(
        Action.MENUITEM, Style.PUSH);
    newMenu.add(menuItem);

    menuItem = (MenuItem) ActionRegistry.get("action:contact").createItem(
        Action.MENUITEM, Style.PUSH);
    newMenu.add(menuItem);

    Action action = ActionRegistry.get("action:new");
    action.setEnabled(true);
    ToolItem item = (ToolItem) action.createItem(Action.TOOLITEM, Style.MENU);
    item.setMenu(newMenu);
    toolbar.add(item);

    toolbar.add(new ToolItem(Style.SEPARATOR));

    item = (ToolItem) ActionRegistry.get("action:disabled").createItem(Action.TOOLITEM,
        Style.PUSH);
    toolbar.add(item);

    toolbar.add(new ToolItem(Style.SEPARATOR));

    item = (ToolItem) ActionRegistry.get("action:appointment").createItem(
        Action.TOOLITEM, Style.PUSH);
    toolbar.add(item);

    item = (ToolItem) ActionRegistry.get("action:contact").createItem(Action.TOOLITEM,
        Style.PUSH);
    toolbar.add(item);

    toolbar.add(new ToolItem(Style.SEPARATOR));

    // TODO
    // item = new ToolItem(Style.TOGGLE);
    // item.setText("Toggle");
    // item.setSelected(true);
    // // item.addSelectionListener(listener);
    // toolbar.add(item);

    ButtonBar buttonbar = new ButtonBar(Style.LEFT);
    buttonbar.add((Button) ActionRegistry.get("action:email").createItem(Action.BUTTON));
    buttonbar.add((Button) ActionRegistry.get("action:appointment").createItem(
        Action.BUTTON));
    buttonbar.add((Button) ActionRegistry.get("action:contact").createItem(Action.BUTTON));

    HorizontalPanel hpanel = new HorizontalPanel();
    hpanel.setSpacing(5);
    hpanel.add(ActionRegistry.get("action:email").createItem(Action.ICONBUTTON));
    hpanel.add(ActionRegistry.get("action:appointment").createItem(Action.ICONBUTTON));
    hpanel.add(ActionRegistry.get("action:contact").createItem(Action.ICONBUTTON));

    WidgetContainer c = new WidgetContainer();
    c.setLayout(new RowLayout());
    c.add(toolbar, new RowData(RowData.FILL_HORIZONTAL));
    c.add(buttonbar, new RowData(RowData.FILL_HORIZONTAL));
    c.add(hpanel);

    container.setLayout(new FillLayout(0));
    container.add(c);
  }

  public void handleAction(BaseEvent pEvent) {
    if (pEvent.widget instanceof Item) {
      Item item = (Item) pEvent.widget;
      String txt = item.getText() != null ? item.getText() : item.getIconStyle();
      Info.show("Item Selection", "Item '{0}' was selected.", txt);
    } else if (pEvent.widget instanceof IconButton) {
      IconButton item = (IconButton) pEvent.widget;
      String txt = item.getStyleName();
      Info.show("Item Selection", "Item '{0}' was selected.", txt);
    }
  }

}
